﻿Public Class ModelClass

    Private _txtWriter As System.IO.StreamWriter
    Private _fileName As String
    Private _className As String
    Private _dtGridview As DataTable

    Public Sub Generate(ByVal lang As String)
        Config.PathFileName = "d:\GenerateModel\" & _className & "\"
        My.Computer.FileSystem.CreateDirectory(Config.PathFileName)

        If lang = "C#" Then
            '*** Generate C# Class ***
            _fileName = Config.PathFileName & _className & "Model.cs"
            _txtWriter = My.Computer.FileSystem.OpenTextFileWriter(_fileName, False)
            _txtWriter.WriteLine(getModelCSharp())
            _txtWriter.Close()

            '*** Generate C# PROCEDURE ***
            _fileName = Config.PathFileName & _className & ".SQL"
            _txtWriter = My.Computer.FileSystem.OpenTextFileWriter(_fileName, False)
            _txtWriter.WriteLine(getStoredProcedureCSharp())
            _txtWriter.Close()


            '*** Generate ASPX Class ***
            _fileName = Config.PathFileName & _className & ".aspx"
            _txtWriter = My.Computer.FileSystem.OpenTextFileWriter(_fileName, False)
            _txtWriter.WriteLine(GenerateExtNet())
            _txtWriter.Close()

            '*** Generate code behind Class ***
            _fileName = Config.PathFileName & _className & ".cs"
            _txtWriter = My.Computer.FileSystem.OpenTextFileWriter(_fileName, False)
            _txtWriter.WriteLine(GenerateCodeBehind())
            _txtWriter.Close()

            '*** Generate UserControl ***
            _fileName = Config.PathFileName & GenerateName(_className) & "Add.ascx"
            _txtWriter = My.Computer.FileSystem.OpenTextFileWriter(_fileName, False)
            _txtWriter.WriteLine(GenerateUserControl())
            _txtWriter.Close()


            _fileName = Config.PathFileName & GenerateName(_className) & "Add.ascx.cs"
            _txtWriter = My.Computer.FileSystem.OpenTextFileWriter(_fileName, False)
            _txtWriter.WriteLine(GenerateUserControlBehind())
            _txtWriter.Close()


        End If

    End Sub

    Private Function getModelCSharp() As String
        Dim now As DateTime = DateTime.Now
        Dim cls As String
        cls = "//**************************************************" & vbCrLf
        cls &= "//CLASS : " & _className & vbCrLf
        cls &= "//AUTHOR : PATTARAWUT CHANNGAM & SUCHIN INPIRUT" & vbCrLf
        cls &= "//LAST MODIFIED : " & now.ToString("D") & vbCrLf
        cls &= "//**************************************************" & vbCrLf & vbCrLf
        cls &= "using System;" & vbCrLf
        cls &= "using System.Collections.Generic;" & vbCrLf
        cls &= "using System.Linq;" & vbCrLf
        cls &= "using System.Text;" & vbCrLf
        cls &= "using System.Data;" & vbCrLf
        cls &= "using System.Data.SqlClient;" & vbCrLf & vbCrLf

        cls &= "namespace Model" & vbCrLf & _
               "{" & vbCrLf & vbCrLf

        cls &= "public class " & _className & vbCrLf
        cls &= "{" & vbCrLf & vbCrLf
        cls &= "     private DBUtil DB;" & vbCrLf
        cls &= "     public string strConn { get; set; }" & vbCrLf

        '*** Generate Attribute ***'
        For i As Integer = 0 To dtGridview.Rows.Count - 1
            cls &= "     public " & MapMethodDataType(dtGridview.Rows(i)("DATA_TYPE").ToString()) & _
                " " & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " {get;set;}" & vbCrLf
        Next
        '*** End Generate Attribute ***'

        '*** Generate Constructor ***'
        cls &= vbCrLf & "     public " & _className & "() {" & vbCrLf
        cls &= "          this.DB = new DBUtil();" & vbCrLf
        cls &= "          DB.strConn = strConn;" & vbCrLf
        cls &= "     }" & vbCrLf
        '*** End Generate Constructor ***' 

        '*** Gernerate Method getById ***'
        cls &= vbCrLf & "     public DataTable getById(string id) {" & vbCrLf & vbCrLf
        cls &= "          DataTable dt = new DataTable();" & vbCrLf
        cls &= "          this.DB = new DBUtil();" & vbCrLf
        cls &= "          DB.strConn = strConn;" & vbCrLf & vbCrLf

        cls &= "          string sql = ""SELECT * FROM " & _className & """ + " & vbCrLf
        cls &= "                       "" WHERE " & dtGridview.Rows(0)("COLUMN_NAME").ToString() & _
                          " = '"" + id + ""'"";" & vbCrLf & vbCrLf
        cls &= "          dt = DB.ExecuteReader(sql);" & vbCrLf & vbCrLf
        cls &= "          return dt;" & vbCrLf & vbCrLf
        cls &= "     }" & vbCrLf
        '*** End Gernerate Method getById ***'


        '*** Gernerate Method getByObject ***'
        cls &= vbCrLf & "     public DataTable getByObject(" & _className & " obj) {" & vbCrLf & vbCrLf
        cls &= "          DataTable dt = new DataTable();" & vbCrLf
        cls &= "          this.DB = new DBUtil();" & vbCrLf
        cls &= "          DB.strConn = strConn;" & vbCrLf & vbCrLf

        cls &= "          string sql = ""SELECT * FROM " & _className & " "" + " & vbCrLf
        cls &= "                       ""WHERE 1 = 1 "";" & vbCrLf & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1
            cls &= "          if(obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " != null && " & vbCrLf & _
                   "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != null && " & vbCrLf & _
                   "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != ""0"")" & vbCrLf

            cls &= "          {" & vbCrLf
            cls &= "               sql += ""AND " & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " LIKE '%"" + obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " + ""%' "";" & vbCrLf
            cls &= "          }" & vbCrLf
        Next

        cls &= "          dt = DB.ExecuteReader(sql);" & vbCrLf & vbCrLf
        cls &= "          return dt;" & vbCrLf & vbCrLf
        cls &= "     }" & vbCrLf
        '*** End Gernerate Method getByObject ***'

        '*** Generate Method insertData ***'
        cls &= vbCrLf & "     public int insertData(" & _className & " obj) {" & vbCrLf & vbCrLf
        cls &= "          this.DB = new DBUtil();" & vbCrLf
        cls &= "          DB.strConn = strConn;" & vbCrLf & vbCrLf
        cls &= "          SqlParameter[] prm;" & vbCrLf
        cls &= "          prm = new SqlParameter[" & dtGridview.Rows.Count - 1 & "];" & vbCrLf & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1

            If i <> 0 Then

                If dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("varchar") Or
                   dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("nvarchar") Then
                    cls &= "          prm[" & i - 1 & "] = new SqlParameter(""@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & _
                       """, SqlDbType." & MapDataType(dtGridview.Rows(i)("DATA_TYPE").ToString()) & "," & _
                       dtGridview.Rows(i)("CHARACTER_MAXIMUM_LENGTH").ToString() & ");" & vbCrLf

                    cls &= "          if(obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != ""0"")" & vbCrLf
                ElseIf dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("decimal") Then
                    cls &= "          prm[" & i - 1 & "] = new SqlParameter(""@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & _
                       """, SqlDbType." & MapDataType(dtGridview.Rows(i)("DATA_TYPE").ToString()) & ");" & vbCrLf
                    cls &= "          prm[" & i - 1 & "].Precision = " & dtGridview.Rows(i)("NUMERIC_PRECISION").ToString() & ";" & vbCrLf
                    cls &= "          prm[" & i - 1 & "].Scale = " & dtGridview.Rows(i)("NUMERIC_PRECISION_RADIX").ToString() & ";" & vbCrLf
                    cls &= "          if(obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != ""0"")" & vbCrLf
                Else
                    cls &= "          prm[" & i - 1 & "] = new SqlParameter(""@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & _
                       """, SqlDbType." & MapDataType(dtGridview.Rows(i)("DATA_TYPE").ToString()) & ");" & vbCrLf
                    cls &= "          if(obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != ""0"")" & vbCrLf
                End If


                cls &= "          {" & vbCrLf
                cls &= "               prm[" & i - 1 & "].Value = obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ";" & vbCrLf
                cls &= "          }" & vbCrLf
                cls &= "          else" & vbCrLf
                cls &= "          {" & vbCrLf
                cls &= "               prm[" & i - 1 & "].Value = DBNull.Value;" & vbCrLf
                cls &= "          }" & vbCrLf
            End If
        Next

        cls &= vbCrLf & "          return DB.ExecuteNonQueryProc(""sp_" & _className & "_insert"", prm, false);" & vbCrLf
        cls &= "     }" & vbCrLf
        '*** End Generate Method insertData ***'

        '*** Generate Method updateData ***'
        cls &= vbCrLf & "     public int updateData(" & _className & " obj) {" & vbCrLf & vbCrLf
        cls &= "          this.DB = new DBUtil();" & vbCrLf
        cls &= "          DB.strConn = strConn;" & vbCrLf & vbCrLf
        cls &= "          SqlParameter[] prm;" & vbCrLf
        cls &= "          prm = new SqlParameter[" & dtGridview.Rows.Count & "];" & vbCrLf & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1

            If dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("varchar") Or
               dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("nvarchar") Then
                cls &= "          prm[" & i & "] = new SqlParameter(""@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & _
                   """, SqlDbType." & MapDataType(dtGridview.Rows(i)("DATA_TYPE").ToString()) & "," & _
                   dtGridview.Rows(i)("CHARACTER_MAXIMUM_LENGTH").ToString() & ");" & vbCrLf
                cls &= "          if(obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != ""0"")" & vbCrLf
            ElseIf dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("decimal") Then
                cls &= "          prm[" & i & "] = new SqlParameter(""@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & _
                   """, SqlDbType." & MapDataType(dtGridview.Rows(i)("DATA_TYPE").ToString()) & ");" & vbCrLf
                cls &= "          prm[" & i & "].Precision = " & dtGridview.Rows(i)("NUMERIC_PRECISION").ToString() & ";" & vbCrLf
                cls &= "          prm[" & i & "].Scale = " & dtGridview.Rows(i)("NUMERIC_PRECISION_RADIX").ToString() & ";" & vbCrLf
                cls &= "          if(obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != ""0"")" & vbCrLf
            Else
                cls &= "          prm[" & i & "] = new SqlParameter(""@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & _
                   """, SqlDbType." & MapDataType(dtGridview.Rows(i)("DATA_TYPE").ToString()) & ");" & vbCrLf
                cls &= "          if(obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != null && " & vbCrLf & _
                           "             obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".ToString() != ""0"")" & vbCrLf
            End If


            cls &= "          {" & vbCrLf
            cls &= "               prm[" & i & "].Value = obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ";" & vbCrLf
            cls &= "          }" & vbCrLf
            cls &= "          else" & vbCrLf
            cls &= "          {" & vbCrLf
            cls &= "               prm[" & i & "].Value = DBNull.Value;" & vbCrLf
            cls &= "          }" & vbCrLf
        Next

        cls &= vbCrLf & "          return DB.ExecuteNonQueryProc(""sp_" & _className & "_update"", prm, false);" & vbCrLf
        cls &= "     }" & vbCrLf
        '*** End Generate Method updateData ***'

        '*** Gernerate Method deleteData ***'
        cls &= vbCrLf & "     public int deleteData(string id) {" & vbCrLf & vbCrLf
        cls &= "          this.DB = new DBUtil();" & vbCrLf
        cls &= "          DB.strConn = strConn;" & vbCrLf & vbCrLf
        cls &= "          SqlParameter[] prm;" & vbCrLf
        cls &= "          prm = new SqlParameter[1];" & vbCrLf & vbCrLf


        cls &= "          prm[0] = new SqlParameter(""@" & dtGridview.Rows(0)("COLUMN_NAME").ToString() & _
               """, id);" & vbCrLf


        cls &= vbCrLf & "          return DB.ExecuteNonQueryProc(""sp_" & _className & "_delete"", prm, false);" & vbCrLf
        cls &= "     }" & vbCrLf
        '*** End Gernerate Method deleteData ***'

        '*** End Class ***'
        cls &= "}" & vbCrLf & vbCrLf

        cls &= "}" & vbCrLf

        Return cls
    End Function

    Private Function getStoredProcedureCSharp() As String
        Dim now As DateTime = DateTime.Now
        Dim cls As String
        '*** Generate Store Procedure inserData ***'
        cls = "       GO" & vbCrLf
        cls &= "       DROP PROCEDURE [dbo].[sp_" & _className & "_insert] " & vbCrLf
        cls &= "       GO" & vbCrLf
        cls &= "       CREATE PROCEDURE [dbo].[sp_" & _className & "_insert] " & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1

            If i <> 0 Then


                cls &= "        "
                If i > 1 Then
                    cls &= ","
                End If

                If dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("varchar") Or
                   dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("nvarchar") Then

                    cls &= "@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " as " & dtGridview.Rows(i)("DATA_TYPE").ToString() & _
                        "(" & dtGridview.Rows(i)("CHARACTER_MAXIMUM_LENGTH").ToString() & ")" & vbCrLf

                ElseIf dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("decimal") Then

                    cls &= "@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " as " & dtGridview.Rows(i)("DATA_TYPE").ToString() & _
                        "(" & dtGridview.Rows(i)("NUMERIC_PRECISION").ToString() & "," & dtGridview.Rows(i)("NUMERIC_PRECISION_RADIX").ToString() & ")" & vbCrLf

                Else
                    cls &= "@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " as " & dtGridview.Rows(i)("DATA_TYPE").ToString() & vbCrLf
                End If
            End If

        Next

        cls &= "       AS" & vbCrLf
        cls &= "       INSERT INTO " & _className & vbCrLf
        cls &= "            (" & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1

            If i <> 0 Then

                cls &= "       "
                If i > 1 Then
                    cls &= ","
                End If

                cls &= dtGridview.Rows(i)("COLUMN_NAME").ToString() & vbCrLf
            End If


        Next

        cls &= "            )" & vbCrLf
        cls &= "       VALUES" & vbCrLf
        cls &= "            (" & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1

            If i <> 0 Then


                cls &= "       "
                If i > 1 Then
                    cls &= ","
                End If

                cls &= "@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & vbCrLf
            End If

        Next

        cls &= "            )" & vbCrLf
        '*** End Generate Store Procedure inserData ***'


        '*** Generate Store Procedure updateData ***'
        cls &= "       GO" & vbCrLf
        cls &= "       DROP PROCEDURE [dbo].[sp_" & _className & "_update] " & vbCrLf
        cls &= "       GO" & vbCrLf
        cls &= vbCrLf & "       CREATE PROCEDURE [dbo].[sp_" & _className & "_update] " & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1

            cls &= "       "
            If i > 0 Then
                cls &= ","
            End If

            If dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("varchar") Or
               dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("nvarchar") Then

                cls &= "@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " as " & dtGridview.Rows(i)("DATA_TYPE").ToString() & _
                    "(" & dtGridview.Rows(i)("CHARACTER_MAXIMUM_LENGTH").ToString() & ")" & vbCrLf

            ElseIf dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("decimal") Then

                cls &= "@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " as " & dtGridview.Rows(i)("DATA_TYPE").ToString() & _
                    "(" & dtGridview.Rows(i)("NUMERIC_PRECISION").ToString() & "," & dtGridview.Rows(i)("NUMERIC_PRECISION_RADIX").ToString() & ")" & vbCrLf

            Else
                cls &= "@" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " as " & dtGridview.Rows(i)("DATA_TYPE").ToString() & vbCrLf
            End If

        Next
        cls &= "       AS" & vbCrLf
        cls &= "       UPDATE " & _className & vbCrLf
        cls &= "       SET" & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1

            If i <> 0 Then


                cls &= "       "
                If i > 1 Then
                    cls &= ","
                End If
                cls &= dtGridview.Rows(i)("COLUMN_NAME").ToString() & " = @" & _
                    dtGridview.Rows(i)("COLUMN_NAME").ToString() & vbCrLf
            End If


        Next
        cls &= "       WHERE " & dtGridview.Rows(0)("COLUMN_NAME").ToString() & " = @" & _
                dtGridview.Rows(0)("COLUMN_NAME").ToString() & vbCrLf
        '*** End Generate Store Procedure updateData ***'

        '*** Generate Store Procedure deleteData ***'
        cls &= "       GO" & vbCrLf
        cls &= "       DROP PROCEDURE [dbo].[sp_" & _className & "_delete] " & vbCrLf
        cls &= "       GO" & vbCrLf

        cls &= vbCrLf & "       CREATE PROCEDURE [dbo].[sp_" & _className & "_delete] " & vbCrLf
        cls &= "       @" & dtGridview.Rows(0)("COLUMN_NAME").ToString() & " as int " & vbCrLf
        cls &= "       AS " & vbCrLf
        cls &= "       BEGIN " & vbCrLf
        cls &= "            DELETE FROM " & _className & " WHERE " & dtGridview.Rows(0)("COLUMN_NAME").ToString() & "=@" & dtGridview.Rows(0)("COLUMN_NAME").ToString() & vbCrLf
        cls &= "       END" & vbCrLf
        '*** End Generate Store Procedure getById ***'



        Return cls
    End Function

    Private Function GenerateExtNet() As String
        Dim cls As String
        cls = "<%@ Page Language=""C#"" AutoEventWireup=""true"" CodeBehind=""p_" & _className & ".aspx.cs"" Inherits=""TCDC_System.Content.MasterData.p_" & _className & """ %>" & vbCrLf
        cls &= "<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" & vbCrLf
        cls &= "<%@ Register Assembly=""Ext.Net"" Namespace=""Ext.Net"" TagPrefix=""ext"" %>" & vbCrLf
        cls &= "<%@ Register TagPrefix=""uc"" Src=""~/UserControl/" & GenerateName(_className) & "Add.ascx"" TagName=""" & GenerateName(_className) & "Add"" %>" & vbCrLf

        cls &= "<html xmlns=""http://www.w3.org/1999/xhtml"">" & vbCrLf
        cls &= "<head runat=""server"">" & vbCrLf

        cls &= "    <script type=""text/javascript"">" & vbCrLf & _
              "          var exportData = function (GridData, GridPanel) {" & vbCrLf & _
              "               GridData.setValue(Ext.encode(GridPanel.getRowsValues({ selectedOnly: false })));" & vbCrLf & _
              "          };" & vbCrLf & _
              "     </script>" & vbCrLf
        cls &= "</head>" & vbCrLf

        cls &= "<body>" & vbCrLf

        cls &= "<form id=""form1"" runat=""server"">" & vbCrLf

        cls &= "<ext:ResourceManager ID=""mainResource"" runat=""server"" CleanResourceUrl=""false"" />" & vbCrLf

        cls &= "<ext:Hidden ID=""GridData"" runat=""server"" />" & vbCrLf

        cls &= "<ext:Viewport ID=""Viewport1"" runat=""server"" Layout=""border"">" & vbCrLf
        cls &= "<Items>" & vbCrLf

        cls &= "<ext:GridPanel ID=""GridPanel" & GenerateName(_className) & """ runat=""server"" Region=""Center"" DDGroup=""gridDDGroup"" " & vbCrLf & _
               "     EnableDragDrop=""false"" StripeRows=""true""  Title=""" & GenerateName(_className) & """ " & vbCrLf & _
               "     Icon=""Vcard"" Frame=""true"">" & vbCrLf

        cls &= "     <TopBar>" & vbCrLf & _
               "          <ext:Toolbar ID=""Toolbar1"" runat=""server"">" & vbCrLf & _
               "               <Items>" & vbCrLf & _
               "                    <ext:Button ID=""btAdd"" runat=""server"" Text=""Add"" Icon=""Add"" OnDirectClick=""Add" & GenerateName(_className) & """/>" & vbCrLf & _
               "                    <ext:ToolbarSeparator/>" & vbCrLf & _
               "                    <ext:Button ID=""btDelete"" runat=""server"" Text=""Delete"" Icon=""Delete"">" & vbCrLf & _
               "                         <DirectEvents>" & vbCrLf & _
               "                              <Click OnEvent=""Delete" & GenerateName(_className) & """ > " & vbCrLf & _
               "                              <EventMask ShowMask=""true""/>" & vbCrLf & _
               "                              </Click>" & vbCrLf & _
               "                         </DirectEvents>" & vbCrLf & _
               "                    </ext:Button>" & vbCrLf & _
               "                    <ext:ToolbarSeparator/>" & vbCrLf & _
               "                    <ext:ToolbarFill ID=""ToolbarFill1"" runat=""server""/>" & vbCrLf & _
               "                    <ext:Button ID=""btExcel"" runat=""server"" AutoPostBack=""true"" OnClick=""ExportExcel" & GenerateName(_className) & """ Text=""To Excel"" Icon=""PageExcel"">" & vbCrLf & _
               "                         <Listeners>" & vbCrLf & _
               "                              <Click Handler=""exportData(#{GridData},#{GridPanel" & GenerateName(_className) & "});""/>" & vbCrLf & _
               "                         </Listeners>" & vbCrLf & _
               "                    </ext:Button>" & vbCrLf & _
               "               </Items>" & vbCrLf & _
               "          </ext:Toolbar>" & vbCrLf & _
               "     </TopBar>" & vbCrLf

        cls &= "     <Store>" & vbCrLf & _
               "          <ext:Store runat=""server"" ID=""Store" & GenerateName(_className) & """ OnRefreshData=""Store" & GenerateName(_className) & "_Refresh""> " & vbCrLf & _
               "               <Reader>" & vbCrLf & _
               "                    <ext:JsonReader IDProperty=""" & dtGridview.Rows(0)("COLUMN_NAME").ToString() & """>" & vbCrLf & _
               "                         <Fields>" & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1







            If dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("datetime") Then
                cls &= "                              <ext:RecordField Name=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ Type=""Date""/>" & vbCrLf

            Else
                cls &= "                              <ext:RecordField Name=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """/>" & vbCrLf

            End If




        Next

        cls &= "                         </Fields>" & vbCrLf & _
               "                    </ext:JsonReader>" & vbCrLf & _
               "               </Reader>" & vbCrLf & _
               "          </ext:Store>" & vbCrLf & _
               "     </Store>" & vbCrLf

        cls &= "     <ColumnModel>" & vbCrLf & _
               "          <Columns>" & vbCrLf & _
               "               <ext:RowNumbererColumn/>" & vbCrLf
        cls &= "               <ext:Column Header=""" & dtGridview.Rows(0)("COLUMN_NAME").ToString() & """"


        cls &= " DataIndex=""" & dtGridview.Rows(0)("COLUMN_NAME").ToString() & """ Width=""100"" Align=""Center""/>" & vbCrLf




        For i As Integer = 0 To dtGridview.Rows.Count - 1
            If i > 0 Then









                If dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("int") Then
                    cls &= "               <ext:Column Header=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & _
       """ DataIndex=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ Width=""100"" Align=""Center"">" & vbCrLf & _
       "                    <Editor>" & vbCrLf


                    cls &= "                         <ext:NumberField ID=""TextField" & i & """ runat=""server"" AllowBlank=""false"" MsgTarget=""Side""/>" & vbCrLf

                    cls &= "                    </Editor>" & vbCrLf & _
      "               </ext:Column>" & vbCrLf
                ElseIf dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("datetime") Then
                    cls &= "               <ext:DateColumn Header=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & _
       """ DataIndex=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ Width=""100"" Align=""Center"" Format=""yyyy-MM-dd"">" & vbCrLf & _
       "                    <Editor>" & vbCrLf


                    cls &= "                         <ext:DateField ID=""TextField" & i & """ runat=""server"" AllowBlank=""false"" MsgTarget=""Side"" Format=""yyyy-MM-dd""/>" & vbCrLf

                    cls &= "                    </Editor>" & vbCrLf & _
          "               </ext:DateColumn>" & vbCrLf
                Else
                    cls &= "               <ext:Column Header=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & _
       """ DataIndex=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ Width=""250"" >" & vbCrLf & _
       "                    <Editor>" & vbCrLf

                    cls &= "                         <ext:TextField ID=""TextField" & i & """ runat=""server"" AllowBlank=""false"" MsgTarget=""Side""/>" & vbCrLf
                    cls &= "                    </Editor>" & vbCrLf & _
      "               </ext:Column>" & vbCrLf
                End If




            End If
        Next

        cls &= "          </Columns>" & vbCrLf & _
               "     </ColumnModel>" & vbCrLf

        cls &= "     <Plugins>" & vbCrLf & _
               "          <ext:GridFilters ID=""GridFilters1"" runat=""server"" Local=""true"">" & vbCrLf & _
               "               <Filters>" & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1
            cls &= "                    <ext:StringFilter DataIndex=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ />" & vbCrLf
        Next

        cls &= "               </Filters>" & vbCrLf & _
               "          </ext:GridFilters>" & vbCrLf & _
               "          <ext:RowEditor ID=""RowEditor" & GenerateName(_className) & """ runat=""server"" SaveText=""Update"">" & vbCrLf & _
               "               <DirectEvents>" & vbCrLf & _
               "                    <AfterEdit OnEvent=""Update" & GenerateName(_className) & """>" & vbCrLf & _
               "                         <ExtraParams>" & vbCrLf & _
               "                              <ext:Parameter Name=""" & GenerateName(_className) & "Value"" Value=""Ext.encode(#{GridPanel" & GenerateName(_className) & "}.getRowsValues({selectedOnly:true}))"" Mode=""Raw"">" & vbCrLf & _
               "                              </ext:Parameter>" & vbCrLf & _
               "                         </ExtraParams>" & vbCrLf & _
               "                         <EventMask ShowMask=""true""/>" & vbCrLf & _
               "                    </AfterEdit>" & vbCrLf & _
               "               </DirectEvents>" & vbCrLf & _
               "          </ext:RowEditor>" & vbCrLf & _
               "     </Plugins>" & vbCrLf

        cls &= "     <SelectionModel>" & vbCrLf & _
               "          <ext:RowSelectionModel ID=""RowSelectionModel1"" runat=""server"" SingleSelect=""true"" />" & vbCrLf & _
               "     </SelectionModel>" & vbCrLf
        cls &= "     <LoadMask ShowMask=""true"" />" & vbCrLf & _
               "     <BottomBar>" & vbCrLf & _
               "          <ext:PagingToolbar ID=""PagingToolbar1"" runat=""server"" PageSize=""19"" DisplayInfo=""true"" " & vbCrLf & _
               "              EmptyMsg=""No Request to display""/>" & vbCrLf & _
               "     </BottomBar>" & vbCrLf
        cls &= "</ext:GridPanel>" & vbCrLf & vbCrLf

        cls &= "<ext:Panel ID=""PanelSouth"" runat=""server"" Region=""South"" Split=""true"" Height=""30"" Padding=""6"" Html="""" BodyStyle=""background-color: #DFE8F6"" />" & vbCrLf
        cls &= "</Items>" & vbCrLf
        cls &= "</ext:Viewport>" & vbCrLf
        cls &= "<uc:" & GenerateName(_className) & "Add ID=""" & GenerateName(_className) & "Add1"" runat=""server"" />" & vbCrLf & vbCrLf
        cls &= "</form>" & vbCrLf
        cls &= "</body>" & vbCrLf
        cls &= "</html>" & vbCrLf


        Return cls
    End Function

    Private Function GenerateCodeBehind() As String
        Dim cls As String

        cls = "using System;" & vbCrLf
        cls &= "using System.Collections.Generic;" & vbCrLf
        cls &= "using System.Linq;" & vbCrLf
        cls &= "using System.Web;" & vbCrLf
        cls &= "using System.Web.UI;" & vbCrLf
        cls &= "using System.Web.UI.WebControls;" & vbCrLf
        cls &= "using Model;" & vbCrLf
        cls &= "using System.Configuration;" & vbCrLf
        cls &= "using Ext.Net;" & vbCrLf
        cls &= "using System.Data;" & vbCrLf
        cls &= "using Newtonsoft.Json;" & vbCrLf
        cls &= "using System.IO;" & vbCrLf
        cls &= "using System.Text;" & vbCrLf & vbCrLf & vbCrLf

        cls &= "namespace TCDC_System.Content.MasterData" & vbCrLf
        cls &= "{" & vbCrLf
        cls &= "public partial class p_" & _className & " : SystemPage.UIPage" & vbCrLf
        cls &= "{" & vbCrLf

        cls &= "private " & _className & " obj" & GenerateName(_className) & ";" & vbCrLf

        cls &= "protected void Page_Init(object sender, EventArgs e)" & vbCrLf
        cls &= "{" & vbCrLf
        cls &= "this." & GenerateName(_className) & "Add1.GridStore = Store" & GenerateName(_className) & ";" & vbCrLf
        cls &= "}" & vbCrLf


        cls &= vbCrLf & "protected void Page_Load(object sender, EventArgs e)" & vbCrLf & _
               "{" & vbCrLf & vbCrLf & _
               "     obj" & GenerateName(_className) & " = new " & _className & "();" & vbCrLf & _
               "     obj" & GenerateName(_className) & ".strConn = ConfigurationManager.AppSettings[""ConnString""]; " & vbCrLf & _
               "     if (!X.IsAjaxRequest)" & vbCrLf & _
               "     {" & vbCrLf & _
               "          Store" & GenerateName(_className) & ".DataSource = Get" & GenerateName(_className) & "();" & vbCrLf & _
               "          Store" & GenerateName(_className) & ".DataBind();" & vbCrLf & _
               "     }" & vbCrLf & _
               "}" & vbCrLf

        cls &= vbCrLf & "protected DataTable Get" & GenerateName(_className) & "()" & vbCrLf & _
               "{" & vbCrLf & _
               "     DataTable dt = obj" & GenerateName(_className) & ".getByObject(new " & _className & "());" & vbCrLf & _
               "     return dt; " & vbCrLf & _
               "}" & vbCrLf

        cls &= vbCrLf & "protected void Add" & GenerateName(_className) & "(object sender, DirectEventArgs e)" & vbCrLf & _
               "{" & vbCrLf & _
               "     " & GenerateName(_className) & "Add1.Show();" & vbCrLf & _
               "}" & vbCrLf

        cls &= vbCrLf & "protected void Update" & GenerateName(_className) & "(object sender, DirectEventArgs e)" & vbCrLf & _
               "{" & vbCrLf & _
               "     string json = e.ExtraParams[""" & GenerateName(_className) & "Value""]; " & vbCrLf & _
               "     Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);" & vbCrLf
        For i As Integer = 0 To dtGridview.Rows.Count - 1
            cls &= "     string " & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " = string.Empty;" & vbCrLf
        Next

        cls &= vbCrLf & "     foreach (Dictionary<string, string> row in workingdays)" & vbCrLf & _
               "     {" & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1
            cls &= "          " & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " = row[""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """].ToString();" & vbCrLf
        Next
        cls &= "     }" & vbCrLf & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1


            If dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("datetime") Or dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("nvarchar") Then
                cls &= "     obj" & GenerateName(_className) & "." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " = " & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ";" & vbCrLf
            ElseIf dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("int") Then

                cls &= "     obj" & GenerateName(_className) & "." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " = Convert.ToInt32(" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ");" & vbCrLf
            Else
                cls &= "     obj" & GenerateName(_className) & "." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " =  Convert.ToDouble(" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ");" & vbCrLf
            End If


        Next

        cls &= "     obj" & GenerateName(_className) & ".updateData(obj" & GenerateName(_className) & ");" & vbCrLf
        cls &= "     Store" & GenerateName(_className) & ".DataSource = Get" & GenerateName(_className) & "();" & vbCrLf & _
               "     Store" & GenerateName(_className) & ".DataBind();" & vbCrLf
        cls &= "}" & vbCrLf

        cls &= vbCrLf & "protected void Delete" & GenerateName(_className) & "(object sender, DirectEventArgs e)" & vbCrLf & _
               "{" & vbCrLf & _
               "     this.ConfirmDelete" & GenerateName(_className) & "();" & vbCrLf & _
               "}" & vbCrLf

        cls &= vbCrLf & "protected void Store" & GenerateName(_className) & "_Refresh(object sender, StoreRefreshDataEventArgs e)" & vbCrLf & _
               "{" & vbCrLf & _
               "     Store" & GenerateName(_className) & ".DataSource = Get" & GenerateName(_className) & "();" & vbCrLf & _
               "     Store" & GenerateName(_className) & ".DataBind();" & vbCrLf & _
               "}" & vbCrLf

        cls &= vbCrLf & "protected void ExportExcel" & GenerateName(_className) & "(object sender, EventArgs e)" & vbCrLf & _
               "{" & vbCrLf & _
               "     string json = GridData.Value.ToString();" & vbCrLf & vbCrLf & _
               "     DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));" & vbCrLf & vbCrLf & _
               "     if(dt.Rows.Count > 0) {" &
               "          Response.Clear();" & vbCrLf & _
               "          Response.AddHeader(""content-disposition"", ""attachment;filename=" & GenerateName(_className) & ".xls"");" & vbCrLf & _
               "          Response.ContentType = ""application/vnd.ms-excel"";" & vbCrLf & _
               "          StringWriter stringWriter = new StringWriter(); " & vbCrLf & _
               "          HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);" & vbCrLf & _
               "          DataGrid dataExportExcel = new DataGrid();" & vbCrLf & _
               "          dataExportExcel.DataSource = dt;" & vbCrLf & _
               "          dataExportExcel.DataBind(); " & vbCrLf & _
               "          dataExportExcel.RenderControl(htmlWrite);" & vbCrLf & _
               "          StringBuilder sbResponseString = new StringBuilder(); " & vbCrLf & _
               "          sbResponseString.Append(""<html xmlns:v=\""urn:schemas-microsoft-com:vml\"" xmlns:o=\""urn:schemas-microsoft-com:office:office\"" xmlns:x=\""urn:schemas-microsoft-com:office:excel\"" xmlns=\""http://www.w3.org/TR/REC-html40\""> <head><meta http-equiv=\""Content-Type\"" content=\""text/html; charset=utf-8\""></head> <body>"");" & vbCrLf & _
               "          sbResponseString.Append(stringWriter + ""</body></html>""); " & vbCrLf & _
               "          sbResponseString.Replace(""&nbsp;"", ""&#160;"");" & vbCrLf & _
               "          Response.Write(sbResponseString.ToString());" & vbCrLf & _
               "          Response.End();" & vbCrLf & _
               "     }" & vbCrLf & _
               "}" & vbCrLf

        cls &= "[DirectMethod(Namespace = ""MyNameSpace"")]" & vbCrLf & _
               "public void ConfirmDelete" & GenerateName(_className) & "()" & vbCrLf & _
               "{" & vbCrLf & _
               "     X.Msg.Confirm(""Confirm"", ""ยืนยันการลบ ?"", new MessageBoxButtonsConfig" & vbCrLf & _
               "     {" &
               "          Yes = new MessageBoxButtonConfig" & vbCrLf & _
               "          {" & vbCrLf & _
               "               Handler = ""MyNameSpace.DeleteData" & GenerateName(_className) & "()"", " & vbCrLf & _
               "               Text = ""Yes""" & vbCrLf & _
               "          }," & vbCrLf & _
               "          No = new MessageBoxButtonConfig { Text = ""No"" }" & vbCrLf & _
               "     }).Show(); " & vbCrLf & _
               "}" & vbCrLf

        cls &= "[DirectMethod(Namespace = ""MyNameSpace"")]" & vbCrLf & _
               "public void DeleteData" & GenerateName(_className) & "()" & vbCrLf & _
               "{" & vbCrLf & _
               "     RowSelectionModel sm = this.GridPanel" & GenerateName(_className) & ".SelectionModel.Primary as RowSelectionModel;" & vbCrLf & _
               "     string " & dtGridview.Rows(0)("COLUMN_NAME").ToString() & " = string.Empty;" & vbCrLf & vbCrLf & _
               "     foreach (SelectedRow row in sm.SelectedRows)" & vbCrLf & _
               "     {" & vbCrLf & _
               "          " & dtGridview.Rows(0)("COLUMN_NAME").ToString() & " = row.RecordID;" & vbCrLf & _
               "     }" & vbCrLf

        cls &= "     obj" & GenerateName(_className) & ".deleteData(" & dtGridview.Rows(0)("COLUMN_NAME").ToString() & ");" & vbCrLf
        cls &= "     Store" & GenerateName(_className) & ".DataSource = Get" & GenerateName(_className) & "();" & vbCrLf
        cls &= "     Store" & GenerateName(_className) & ".DataBind();" & vbCrLf
        cls &= "}" & vbCrLf
        cls &= "}" & vbCrLf
        cls &= "}" & vbCrLf

        Return cls
    End Function

    Private Function GenerateUserControl() As String
        Dim cls As String

        cls = "<%@ Control Language=""C#"" AutoEventWireup=""true"" CodeBehind=""" & GenerateName(_className) & "Add.ascx.cs"" " & _
               "Inherits=""TCDC_System.UserControl." & GenerateName(_className) & "Add"" %>" & vbCrLf & vbCrLf
        cls &= "<%@ Register Assembly=""Ext.Net"" Namespace=""Ext.Net"" TagPrefix=""ext"" %>" & vbCrLf & vbCrLf
        cls &= "<style type=""text/css"">" & vbCrLf & _
               "     .icon-exclamation" & vbCrLf & _
               "     {" & vbCrLf & _
               "          padding-left: 25px !important;" & vbCrLf & _
               "          background: url(../../Attribute/images/icons/System/exclamation.png) no-repeat 3px 3px !important; " & vbCrLf & _
               "     }" & vbCrLf & _
               "     .icon-accept" & vbCrLf & _
               "     {" & vbCrLf & _
               "          padding-left: 25px !important;" & vbCrLf & _
               "          background: url(../../Attribute/images/icons/System/accept.png) no-repeat 3px 3px !important;" & vbCrLf & _
               "     }" & vbCrLf & _
               "</style>" & vbCrLf

        cls &= "<ext:Window ID=""Window" & GenerateName(_className) & "Add"" runat=""server"" Icon=""Group"" Title=""" & GenerateName(_className) & """ " & vbCrLf & _
               "     Width=""600"" Height=""300"" AutoShow=""false"" Modal=""true"" Hidden=""true"" Layout=""Fit"">" & vbCrLf
        cls &= "     <TopBar>" & vbCrLf & _
               "          <ext:Toolbar ID=""Toolbar1"" runat=""server"">" & vbCrLf & _
               "               <Items>" & vbCrLf & _
               "                    <ext:ToolbarFill ID=""ToolbarFill1"" runat=""server""/>" & vbCrLf & _
               "                    <ext:Button ID=""btnSaveRecord"" runat=""server"" Icon=""Disk"" Text=""Save"">" & vbCrLf & _
               "                         <DirectEvents>" & vbCrLf & _
               "                              <Click OnEvent=""Save" & GenerateName(_className) & """ Before=""return #{FormPanel1}.getForm().isValid();""> " & vbCrLf & _
               "                                   <EventMask ShowMask=""true""/>" & vbCrLf & _
               "                              </Click>" & vbCrLf & _
               "                         </DirectEvents>" & vbCrLf & _
               "                    </ext:Button>" & vbCrLf & _
               "               </Items>" & vbCrLf & _
               "          </ext:Toolbar>" & vbCrLf & _
               "     </TopBar>" & vbCrLf
        cls &= "     <Items>" & vbCrLf
        cls &= "          <ext:FormPanel ID=""FormPanel1""" & vbCrLf & _
               "               runat = ""server""" & vbCrLf & _
               "               Padding = ""5""" & vbCrLf & _
               "               Frame = ""true""" & vbCrLf & _
               "               LabelWidth = ""150""" & vbCrLf & _
               "               MonitorValid = ""true""" & vbCrLf & _
               "               Width = ""500""" & vbCrLf & _
               "               DefaultAnchor = ""-20""" & vbCrLf & _
               "               Height=""230"">" & vbCrLf
        cls &= "               <Items>" & vbCrLf
        For i As Integer = 0 To dtGridview.Rows.Count - 1

            If i <> 0 Then
                If dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("int") Then
                    cls &= "                    <ext:NumberField ID=""txt_" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ runat=""server"" FieldLabel=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ Width=""300"" " & vbCrLf & _
             "                         AllowBlank=""false"" MsgTarget=""Title"" />" & vbCrLf

                ElseIf dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("datetime") Then
                    cls &= "                    <ext:DateField ID=""txt_" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ runat=""server"" FieldLabel=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ Width=""300"" " & vbCrLf & _
             "                         AllowBlank=""false"" MsgTarget=""Title"" />" & vbCrLf


                Else
                    cls &= "                    <ext:TextField ID=""txt_" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ runat=""server"" FieldLabel=""" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & """ Width=""300"" " & vbCrLf & _
             "                         AllowBlank=""false"" MsgTarget=""Title"" />" & vbCrLf

                End If


            End If
        Next
        cls &= "               </Items>" & vbCrLf
        cls &= "               <BottomBar>" & vbCrLf & _
               "                    <ext:StatusBar ID=""StatusBar1"" runat=""server"" Height=""25""/>" & vbCrLf & _
               "               </BottomBar>" & vbCrLf
        cls &= "               <Listeners>" & vbCrLf & _
               "                    <ClientValidation Handler=""this.getBottomToolbar().setStatus({text : valid ? 'Form is valid' : 'Form is invalid', iconCls: valid ? 'icon-accept' : 'icon-exclamation'});""/>" & vbCrLf & _
               "               </Listeners>" & vbCrLf
        cls &= "          </ext:FormPanel>" & vbCrLf
        cls &= "     </Items>" & vbCrLf
        cls &= "</ext:Window>" & vbCrLf
        Return cls
    End Function


    Private Function GenerateUserControlBehind() As String
        Dim cls As String

        cls = "using System;" & vbCrLf
        cls &= "using System.Collections.Generic;" & vbCrLf
        cls &= "using System.Linq;" & vbCrLf
        cls &= "using System.Web;" & vbCrLf
        cls &= "using System.Web.UI;" & vbCrLf
        cls &= "using System.Web.UI.WebControls;" & vbCrLf
        cls &= "using Model;" & vbCrLf
        cls &= "using System.Configuration;" & vbCrLf
        cls &= "using Ext.Net;" & vbCrLf

        cls &= "namespace TCDC_System.UserControl" & vbCrLf
        cls &= "{" & vbCrLf
        cls &= "public partial class " & GenerateName(_className) & "Add : System.Web.UI.UserControl"
        cls &= "{" & vbCrLf
        cls &= "private " & _className & " obj;" & vbCrLf
        cls &= vbCrLf & "protected void Page_Load(object sender, EventArgs e)" & vbCrLf & _
               "{" & vbCrLf & _
               "     obj = new " & _className & "();" & vbCrLf & _
               "     obj.strConn = ConfigurationManager.AppSettings[""ConnString""];" & vbCrLf & _
               "}" & vbCrLf
        cls &= vbCrLf & "public void Show()" & vbCrLf & _
               "{" & vbCrLf & _
               "     Window" & GenerateName(_className) & "Add.Show(); " & vbCrLf & _
               "}" & vbCrLf
        cls &= vbCrLf & "protected void Save" & GenerateName(_className) & "(object sender, DirectEventArgs e)" & vbCrLf & _
               "{" & vbCrLf

        For i As Integer = 0 To dtGridview.Rows.Count - 1
            If i <> 0 Then
                If dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("datetime") Or dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("nvarchar") Then
                    cls &= "     obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " = txt_" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".Text;" & vbCrLf

                ElseIf dtGridview.Rows(i)("DATA_TYPE").ToString().Equals("int") Then

                    cls &= "     obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " = Convert.ToInt32(txt_" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".Text);" & vbCrLf

                Else
                    cls &= "     obj." & dtGridview.Rows(i)("COLUMN_NAME").ToString() & " =  Convert.ToDouble(txt_" & dtGridview.Rows(i)("COLUMN_NAME").ToString() & ".Text);" & vbCrLf

                End If


            End If




         

        Next

        cls &= "     obj.insertData(obj);" & vbCrLf

        cls &= "     GridStore.DataSource = obj.getByObject(new " & _className & "());" & vbCrLf & _
               "     GridStore.DataBind();" & vbCrLf

        cls &= "     FormPanel1.Reset();" & vbCrLf & _
               "     Window" & GenerateName(_className) & "Add.Hide();" & vbCrLf
        cls &= "}" & vbCrLf

        cls &= "public Store GridStore { get; set; } " & vbCrLf
        cls &= "}" & vbCrLf
        cls &= "}" & vbCrLf
        Return cls
    End Function


    Private Function GenerateName(ByVal name As String) As String
        Dim str As String = String.Empty
        Dim strs As String()
        strs = name.Split("_")

        Dim i As Integer = 0
        For Each word As String In strs
            If i > 1 Then
                str &= word.Substring(0, 1).ToUpper() & word.Substring(1, word.Length - 1).ToLower()
            End If
            i += 1
        Next

        Return str
    End Function

    Private Function MapDataType(ByVal field As String) As String
        Select Case field
            Case "int"
                Return "Int"
            Case "varchar"
                Return "VarChar"
            Case "nvarchar"
                Return "NVarChar"
            Case "decimal"
                Return "Decimal"
            Case "date"
                Return "Date"
            Case "datetime"
                Return "DateTime"
            Case Else
                Return "NVarChar"
        End Select
        Return ""
    End Function

    Private Function MapMethodDataType(ByVal field As String) As String
        Select Case field
            Case "int"
                Return "int"
            Case "varchar"
                Return "string"
            Case "nvarchar"
                Return "string"
            Case "decimal"
                Return "double"
            Case Else
                Return "string"
        End Select
        Return ""
    End Function

    Public Property className() As String
        Get
            Return _className
        End Get
        Set(ByVal value As String)
            _className = value
        End Set
    End Property

    Public Property dtGridview() As DataTable
        Get
            Return _dtGridview
        End Get
        Set(ByVal value As DataTable)
            _dtGridview = value
        End Set
    End Property
End Class
